<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\Common\Constants\StatusConstants;
use App\CustHub\Model\ChCtrlRoomSchemeCfg;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;

class ChCtrlRoomSchemeCfgMapper extends AbstractMapper
{

    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChCtrlRoomSchemeCfg::class;
    }


//    #[Cacheable(prefix: 'ctrl_room_scheme_info',value: '#{robotId}',ttl: 15 * 60)]
    public function getSchemeListByRobot(int $robotId): array|\Hyperf\Database\Model\Collection
    {
        $list = $this->model::query()
            ->with(['scheme' => function ($query) {
                $query->select(['id', 'room_wxid','listening_content','is_auto_at_cust']);
            }])
            ->where('robot_id', $robotId)
            ->whereHas('scheme',function (Builder $query){
                $query->where('status', StatusConstants::ENABLE);
            })
            ->get();
        return $list;
    }


    public function delCache(int $robotId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'ctrl_room_scheme_info:'.$robotId;
        $redis->del([$keyOne]);
    }
}